package com.yj.shipxy.service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yj.shipxy.entity.QueryShip;
import com.yj.shipxy.entity.ShipPosition;
import com.yj.shipxy.entity.ShipTrack;
import com.yj.util.HttpUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengguanyong on 2017/12/22.
 */
@Service
public class ShipxyService {
    private static final String SKEY = "7fe28a0f7dd74ae1a42732bf72fec752"; //船讯网key
    private String match_url = "http://api.shipxy.com/apicall/QueryShip?v=2&k="+SKEY+"&enc=1&kw=";
    private String single_url = "http://api.shipxy.com/apicall/GetSingleShip?v=2&k="+SKEY+"&enc=1&id=";
    private String track_url = "http://api.shipxy.com/apicall/GetShipTrack?v=2&k="+SKEY+"&enc=1&cut=0&id=";

    public Integer matchShipId(String keyword) {
        JSONObject jsonObject = HttpUtil.httpGet(match_url+keyword+"&max=1");
        if ((int)jsonObject.get("status") == 0) {
            JSONArray jsonArray = (JSONArray)jsonObject.get("data");
            String shipJson = jsonArray.get(0).toString();
            Gson gson = new Gson();
            QueryShip queryShip = gson.fromJson(shipJson,QueryShip.class);
            return queryShip.getShipID();
        }
        return 0;
    }

    public ShipPosition getSingleShip(Integer ShipId) {
        JSONObject jsonObject = HttpUtil.httpGet(single_url+ShipId+"&idtype=0");
        if ((int)jsonObject.get("status") == 0) {
            JSONArray jsonArray = (JSONArray)jsonObject.get("data");
            if (jsonArray.size() == 0) {
                return null;
            }
            String shipPositionJson = jsonArray.get(0).toString();
            Gson gson = new Gson();
            ShipPosition shipPosition = gson.fromJson(shipPositionJson,ShipPosition.class);
            return shipPosition;
        }
        return null;
    }

    public List<ShipTrack> getShipTrackList(Integer ShipId) {
        List<ShipTrack> shipTrackList = new ArrayList<>();
        JSONObject jsonObject = HttpUtil.httpGet(track_url+ShipId+"&btm=1514000979&etm=1514173779");
        if ((int)jsonObject.get("status") == 0) {
            JSONArray jsonArray = (JSONArray)jsonObject.get("points");
            if (jsonArray.size() == 0) {
                return null;
            }
            String shipTrackJson = jsonArray.toString();
            Gson gson = new Gson();
            shipTrackList = gson.fromJson(shipTrackJson, new TypeToken<List<ShipTrack>>(){}.getType());
            return shipTrackList;
        }
        return shipTrackList;
    }

    public ShipPosition getPositionByKeyword(String keyword) {
        Integer ShipId = matchShipId(keyword);
        ShipPosition shipPosition = getSingleShip(ShipId);
        return shipPosition;
    }

    public List<ShipTrack> getShipJournal(String keyword) {
        Integer ShipId = matchShipId(keyword);
        List<ShipTrack> shipTrackList = getShipTrackList(ShipId);
        return shipTrackList;
    }


    public static void main(String[] args) {
//        ShipxyService shipxyService = new ShipxyService();
//        System.out.println(shipxyService.matchShipId("9387164"));
//        System.out.println(shipxyService.getSingleShip(357132000).toString());

//        System.out.println(shipxyService.getPositionByKeyword("9387164"));
//        System.out.println(shipxyService.getShipJournal("9387164"));

        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(1514000980,0,ZoneOffset.ofHours(8));
        System.out.println(localDateTime);

    }
}
